﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using System.Windows.Threading;

namespace PermDoc
{
    public partial class Settings : Page
    {

        #region Members
        private bool IsInit = false;
        protected static DispatcherTimer _timer_idle = new DispatcherTimer();
        private Uri _WelcomeUri = new Uri(@"/Welcome.xaml", UriKind.Relative);
        #endregion

        #region Constructors
        public Settings()
        {
            InitializeComponent();
        }
        #endregion

        #region Page Events
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            App.IdleEmpty();
            oInvalidateNightCheckBox.IsChecked = App._IsRefreshNightOnly;
//#if DEBUG
            oInvalidateNightCheckBox.IsEnabled = true;
//#else
//            oInvalidateNightCheckBox.IsEnabled = false;
//#endif

            switch(App.LoadingMonthMaxCount)
            {
                case 3:
                    oMonthCount.SelectedIndex = 0;
                    break;
                case 6:
                    oMonthCount.SelectedIndex = 1;
                    break;
                case 8:
                    oMonthCount.SelectedIndex = 2;
                    break;
                case 12:
                    oMonthCount.SelectedIndex = 3;
                    break;
                case 16:
                    oMonthCount.SelectedIndex = 4;
                    break;
                default:
                    oMonthCount.SelectedIndex = 0;
                    break;
            }

        }
        private void LayoutRoot_Unloaded(object sender, RoutedEventArgs e)
        {
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.IdleEmpty();
            _timer_idle.Tick += new EventHandler(myTimer_Idle);
            _timer_idle.Interval = new TimeSpan(0, 0, 1);
            _timer_idle.Start();
            IsInit = true;
        }
        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            _timer_idle.Stop();
            IsInit = false;
        }
        private void Page_MouseMove(object sender, MouseEventArgs e)
        {
            App.IdleEmpty();
        }
        void myTimer_Idle(object sender, EventArgs e)
        {
            if (App.Idle > App._idleMax)
            {
                if (App.MainPagePtr.navFrame.Source != _WelcomeUri)
                {
                    App.MainPagePtr.navFrame.Source = _WelcomeUri;
                }
            }
        }
        #endregion

        #region Controls Events
        private void oInvalidateNightCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            App._IsRefreshNightOnly = true;
        }
        private void oInvalidateNightCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            App._IsRefreshNightOnly = false;
        }
        private void oMonthCount_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DateTime dtNow = DateTime.Now;
            dtNow = new DateTime(dtNow.Year, dtNow.Month, 1);
            DateTime dtBefore = dtNow;
            switch(oMonthCount.SelectedIndex)
            {
                case 0:
                    App.LoadingMonthMaxCount = 3;
                    dtBefore = dtBefore.AddMonths(-3);
                    dtBefore = new DateTime(dtBefore.Year, dtBefore.Month, 1);
                    App.LoadingDateMax = dtBefore;
                    break;
                case 1:
                    App.LoadingMonthMaxCount = 6;
                    dtBefore = dtBefore.AddMonths(-6);
                    dtBefore = new DateTime(dtBefore.Year, dtBefore.Month, 1);
                    App.LoadingDateMax = dtBefore;
                    break;
                case 2:
                    App.LoadingMonthMaxCount = 8;
                    dtBefore = dtBefore.AddMonths(-8);
                    dtBefore = new DateTime(dtBefore.Year, dtBefore.Month, 1);
                    App.LoadingDateMax = dtBefore;
                    break;
                case 3:
                    App.LoadingMonthMaxCount = 12;
                    dtBefore = dtBefore.AddMonths(-12);
                    dtBefore = new DateTime(dtBefore.Year, dtBefore.Month, 1);
                    App.LoadingDateMax = dtBefore;
                    break;
                case 4:
                    App.LoadingMonthMaxCount = 16;
                    dtBefore = dtBefore.AddMonths(-16);
                    dtBefore = new DateTime(dtBefore.Year, dtBefore.Month, 1);
                    App.LoadingDateMax = dtBefore;
                    break;
                default:
                    App.LoadingMonthMaxCount = 3;
                    dtBefore = dtBefore.AddMonths(-3);
                    dtBefore = new DateTime(dtBefore.Year, dtBefore.Month, 1);
                    App.LoadingDateMax = dtBefore;
                    break;
            }
        }
        #endregion

    }
}
